<?php
namespace Home\Controller;
use Think\Controller;
class DateController extends Controller {
	
    public function index(){
        $users = M('user');
        $dailyReport = M('daily_report');
        $reports = $dailyReport->order('create_time DESC')->select();
        for($i = 0; $i < count($reports); $i ++ ){
            $value = $reports[$i];
            $splits = explode("-",$value['create_time']);
            $reports[$i]['month'] = $splits[1];
            $reports[$i]['day'] = $splits[2];
            $reports[$i]['user_name'] = $users->where(array('id' => $reports[$i]['user_id']))->find()['name'];
            if($reports[$i]['create_time'] == date('Y-m-d'))
                $reports[$i]['today'] = true;
            else
                $reports[$i]['today'] = false;
        }
        $this->assign('reports',$reports);
    	$this->display();
    }

    public function add(){
    	$this->display();
    }

    public function reply_list(){
        $dailyReport = M('daily_report');
        $comments = M('comment');
        $commentData = $comments->select();
        for ($i=0; $i < count($commentData); $i++) { 
            $userId = $commentData[$i]['user_id'];
            $commentData[$i]['name'] = $this->getUserName($userId);
            $otherId = $commentData[$i]['other_id'];
            $commentData[$i]['artical_title'] = $dailyReport->where(array('id'=>$commentData[$i]['report_id']))->find()['title'];
            if(!isset($other_id))
                $commentData[$i]['other_name'] = $this->getUserName($otherId);
            else
                $commentData[$i]['other_name'] = 'NULL';
        }
        // dump($commentData);
        $this->assign('item',$modifyItem);
        $this->assign('comments',$commentData);
        $this->display();
    }

    public function addComment(){
        $data = $_POST;
        if($data['other_name'] != 'NULL'){
            $data['other_id'] = M('user')->where(array('name' => $data['otherName']))->find()['id'];
        }
        M('comment')->add($data);
        echo json_encode(array('result' => '添加成功'));
    }

    public function edit(){
        $id = $_GET['id'];
        $dailyReport = M('daily_report');
        $item = $dailyReport->where(array('id' => $id))->find();
        $this->assign('item',$item);
        $this->display();
    }

    public function content()
    {
        $id = $_GET['id'];
        $dailyReport = M('daily_report');
        $comments = M('comment');
        $modifyItem = $dailyReport->where(array('id' => $id))->find();
        $modifyItem['user_name'] = $this->getUserName($modifyItem['user_id']);
        $commentData = $comments->where(array('report_id' => $id))->select();
        for ($i=0; $i < count($commentData); $i++) { 
            $userId = $commentData[$i]['user_id'];
            $commentData[$i]['name'] = M('user')->where(array('id' => $userId))->find()['name'];
            $otherId = $commentData[$i]['other_id'];
            if(!isset($other_id))
                $commentData[$i]['other_name'] = M('user')->where(array('id' => $otherId))->find()['name'];
            else
                $commentData[$i]['other_name'] = 'NULL';
        }
        // dump($commentData);
        $this->assign('item',$modifyItem);
        $this->assign('comments',$commentData);
        $this->display();
    }

    public function del()
    {   
        $id = $_GET['id'];
        $dailyReport = M('daily_report');
        $dailyReport->where(array('id' => $id))->delete();
        $this->success('删除成功',U('Date/index'));
    }

    public function addReport()
    {
        //现在这边用户默认为
        $dailyReport = M('daily_report');
        $data = $_POST;
        $data['user_id'] = 1;
        $data['create_time'] = date('Y-m-d');
        $dailyReport->data($data)->add();
        $this->success('新增成功',U('Date/index'));
    }

    public function getUserName($userId = 1){
        $users = M('user');
        return M('user')->where(array('id' => $userId))->find()['name'];
    }

    public function test()
    {
        $dailyReport = M('daily_report');
        $reports = $dailyReport->select();
        // foreach ($reports as $key => $value) {            
        //     $splits = explode("-",$value['create_time']);
        //     $value['month'] = $splits[0];
        //     $value['day'] = $splits[1];
        //     dump($value);
        // }
        // dump($reports);

        for($i = 0; $i < count($reports); $i ++ ){
            $value = $reports[$i];
            $splits = explode("-",$value['create_time']);
            $reports[$i]['month'] = $splits[1];
            $reports[$i]['day'] = $splits[2];
        }
        dump($reports);
    }
}